<?php

session_start();
require_once('connect.php');
require_once('includes/user.class.php');
require_once('includes/courses.class.php');

$database = Database::getInstance();
$courses = new Courses();
$user = new User();

if(!$user->is_admin()) header('Location: index.php');
require_once('partials/header.php'); //wczytaj header i menu
?>


    <div class="container">

        <div class="row">

            <div class="col-md-3">

                <?php require_once('partials/content-left-menu.php'); ?>

            </div>

            <div class="col-md-9">

                <h2>Dodaj kierunki studiów</h2>

				<div class="subject-table">
					<div class="subject-table-header subject-table-row">
						<div class="subject-table-header-cell">Wydział</div>
						<div class="subject-table-header-cell">Kierunek</div>
						<div class="subject-table-header-cell">Rok</div>
					</div>
					
					<?php
					$coursesData = $courses -> get_courses();
					foreach($coursesData as $course){
						print '<div class="subject-table-row">';
						print'<div class="subject-table-header-cell">'.$course['department'].'</div>';
						print'<div class="subject-table-header-cell">'.$course['direction'].'</div>';
						print'<div class="subject-table-header-cell">'.$course['year'].'</div>';
						print '</div>';
					}
					?>
				</div>
				<!--TYLKO DLA ADMINOW -->
				<?php

                if($user->is_admin()){
                    if(isset($_POST['submit'])){
                        $department = $_POST['department'];
                        $direction = $_POST['direction'];
                        $year = $_POST['year'];

                        if($courses->check_course($department,$direction,$year)){
                            $add = $courses->add_course($department,$direction,$year);
                            if($add > 0){
                                header( 'Location:study_courses.php');
                            }else{
                                $alert = array('Nie dodano kierunku', 'danger');
                            }
                        }else{
                            $alert = array('Taki kierunek już istnieje', 'success');
                        }
                        $user->show_alert($alert);
                    }
                    ?>
                    <div class="add-news-subject-area">
                        <form role="form" action = "study_courses.php" method = "POST">
                            <input type="text" name="department" class="subject-input" placeholder="Wydział">
                            <input type="text" name="direction" class="subject-input" placeholder="Kierunek">
                            <input type="text" name="year" class="subject-input" placeholder="Rok">
                            <input type="hidden" name="addCourse" value="true">
                            <input type="submit" name="submit" value="Dodaj kierunek studiów do bazy" class="subject-button">
                        </form>
                    </div>
                    <!--TYLKO DLA ADMINOW -->
                <?php
                }
                ?>
            </div>

        </div>

    </div>

 
<?php
require_once('partials/footer.php');
?>
